var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var wxParse = require('../../lib/wxParse/wxParse.js');
var app = getApp();

Page({
  data: {
    sceneList: [],
    sceneAll: [],
    banner: [],
    city: "成都",
    pageSize: 10,
    pageNum: 1
  },
  getData: function () {
    let that = this;
    var data = {
      pageSize: that.data.pageSize,
      pageNum: that.data.pageNum
    }
    util.request(api.SceneIndex, data).then(function (res) {
      console.log(res)
      if (res.code === '200') {
        var noMore = !(res.datas.sceneAll && res.datas.sceneAll.length > 0)
        var noMore1 = !(res.datas.sceneList && res.datas.sceneList.length > 0);
        that.setData({
          sceneList: res.datas.sceneList,
          banner: res.datas.banner,
          sceneAll: res.datas.sceneAll,
          noMore: noMore,
          noMore1: noMore1,
          pageNum: 1
        });
        //  wxParse.wxParse('article', 'html', article, that, 5);
      }
    });
  },

  onLoad: function (options) {
    app.init(options, this)
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面初始化 options为页面跳转所带来的参数
    this.getData();
    app.getShareInfo(this);
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  search: function () {
    wx.navigateTo({
      url: '/pages/hotelList/index'
    })
  },
  selectCity: function () {
    wx.navigateTo({
      url: '/pages/city/index'
    })
  },
  onPullDownRefresh: function (e) {
    this.getData();
  },
  onReachBottom() {
    //获取更多数据
    this.getMoreData();
  },
  //获取更多数据
  getMoreData: function (e) {
    let that = this;
    var sceneAll = that.data.sceneAll;
    var pageNum = that.data.pageNum;
    var data = {
      pageSize: that.data.pageSize,
      pageNum: pageNum + 1
    };
    wx.showLoading({
      title: '加载中..',
      mask: true
    });
    util.request(api.SceneSearchList, data).then(function (res) {
      wx.hideLoading();
      if (res.code === '200') {
        if (res.datas && res.datas.length > 0) {
          sceneAll = sceneAll.concat(res.datas)
          that.setData({
            sceneAll: sceneAll,
            pageNum: pageNum + 1
          });
        } else {
          that.setData({
            noMore: true
          })
        }
      } else {
        wx.showToast({
          title: '系统错误',
        })
      }

    });
  },
  fnToIndex: function () {
    wx.switchTab({
      url: '/pages/index/index-new',
    })
  },
  fnShare: function (e) {
    var that = this;
    var path = "";
    var type = e.currentTarget.dataset.type;
    var goodsId = e.currentTarget.dataset.id;
    var imgUrl = e.currentTarget.dataset.url;
    var goodsName = e.currentTarget.dataset.name;
    var address = e.currentTarget.dataset.address;
    var price = e.currentTarget.dataset.price;
    var marketPrice = e.currentTarget.dataset.marketprice;
    if (type == 'hotel') {
      path = "/pages/hotelDetail/hoteldetail";
    } else if (type == "scene") {
      path = "/pages/sceneDetail/sceninfo";
    } else {
      path = "/pages/tourDetail/travelline"
    }
    app.fnShareGoods(path, goodsId, imgUrl, goodsName, type, address, price,marketPrice);
  }

})